/*
 * 文件名: PwdKeyboardActivity.java
 * 版    权：  Copyright PingAn Technology All Rights Reserved.
 * 描    述: [该类的简要描述]
 * 创建人: EX-LIUHONGBAO001
 * 创建时间: 2011-12-28
 * 
 * 修改人：
 * 修改时间:
 * 修改内容：[修改内容]
 */
package app.widget;

import android.app.Activity;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.os.Bundle;
import android.text.Editable;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;

import com.longevityassistant.activity.R;

/**
 * @author EX-LIUHONGBAO001
 * @date 2011-12-28
 * @version [Android PABank C01, @2011-12-28]
 * @description
 */
public class PwdKeyboardActivity extends Activity {
	/**
	 * 
	 */
	KeyboardView keyboardView;
	/**
	 * 
	 */
	EditText edit;
	/**
	 * 
	 */
	EditText edit1;
	/**
	 * 
	 */
	EditText ed;

	/**
	 * [一句话功能简述]<BR>  使用时在自己的控件获取焦点时调用改类的showKeyboard()方法即可。
	 * [功能详细描述]
	 * 
	 * @param savedInstanceState
	 * @see android.app.ActivityGroup#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.app_widget_password_keyboard);
		edit = (EditText) this.findViewById(R.id.edit);

		edit1 = (EditText) this.findViewById(R.id.edit1);

		edit.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				ed = edit;
				showKeyboard();
				return false;
			}
		});

		edit1.setOnTouchListener(new OnTouchListener() {

			/**
			 * [一句话功能简述]<BR>
			 * [功能详细描述]
			 * 
			 * @param v
			 * @param event
			 * @return
			 * @see android.view.View.OnTouchListener#onTouch(android.view.View,
			 *      android.view.MotionEvent)
			 */
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				ed = edit1;
				showKeyboard();
				return false;
			}
		});

		keyboardView = (KeyboardView) findViewById(R.id.keyboard_view);
		keyboardView.setKeyboard(new Keyboard(this, R.xml.qwerty));
		keyboardView.setEnabled(true);
		keyboardView.setPreviewEnabled(true);
		keyboardView
				.setOnKeyboardActionListener(new OnKeyboardActionListener() {
					@Override
					public void onKey(int primaryCode, int[] keyCodes) {
						Editable editable = ed.getText();
						int start = ed.getSelectionStart();
						if (primaryCode == Keyboard.KEYCODE_CANCEL) {
							hideKeyboard();
						} else if (primaryCode == Keyboard.KEYCODE_DELETE) {
							if (editable != null && editable.length() > 0) {
								editable.delete(start - 1, start);
							}
						} else if (primaryCode == 57419) { // 光标移动到左边
							if (start > 0) {
								ed.setSelection(start - 1);
							}
						} else if (primaryCode == 57421) { // 光标移动到右边
							if (start < ed.length()) {
								ed.setSelection(start + 1);
							}
						} else {
							editable.insert(start,
									Character.toString((char) primaryCode));
						}
					}

					/**
					 * [一句话功能简述]<BR>
					 * [功能详细描述]
					 * 
					 * @param primaryCode
					 * @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#onPress(int)
					 */
					/**
					 * [一句话功能简述]<BR>
					 * [功能详细描述]
					 * 
					 * @param primaryCode
					 * @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#onPress(int)
					 */
					@Override
					public void onPress(int primaryCode) {

					}

					/**
					 * [一句话功能简述]<BR>
					 * [功能详细描述]
					 * 
					 * @param primaryCode
					 * @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#onRelease(int)
					 */
					@Override
					public void onRelease(int primaryCode) {

					}

					/**
					 * [一句话功能简述]<BR>
					 * [功能详细描述]
					 * 
					 * @param text
					 * @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#onText(java.lang.CharSequence)
					 */
					/**
					 * [一句话功能简述]<BR>
					 * [功能详细描述]
					 * 
					 * @param text
					 * @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#onText(java.lang.CharSequence)
					 */
					@Override
					public void onText(CharSequence text) {

					}

					/**
					 * [一句话功能简述]<BR>
					 * [功能详细描述]
					 * 
					 * @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#swipeDown()
					 */
					@Override
					public void swipeDown() {

					}

					/**
					 * [一句话功能简述]<BR>
					 * [功能详细描述]
					 * 
					 * @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#swipeLeft()
					 */
					@Override
					public void swipeLeft() {

					}

					/**
					 * [一句话功能简述]<BR>
					 * [功能详细描述]
					 * 
					 * @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#swipeRight()
					 */
					@Override
					public void swipeRight() {

					}

					/**
					 * [一句话功能简述]<BR>
					 * [功能详细描述]
					 * 
					 * @see android.inputmethodservice.KeyboardView.OnKeyboardActionListener#swipeUp()
					 */
					@Override
					public void swipeUp() {

					}

				});
	}

	/**
	 * [显示键盘]<BR>
	 * [显示键盘]
	 */
	private void showKeyboard() {
		int visibility = keyboardView.getVisibility();
		if (visibility == View.GONE || visibility == View.INVISIBLE) {
			keyboardView.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * [隐藏键盘]<BR>
	 * [隐藏键盘]
	 */
	private void hideKeyboard() {
		int visibility = keyboardView.getVisibility();
		if (visibility == View.VISIBLE) {
			keyboardView.setVisibility(View.INVISIBLE);
		}
	}
}
